<?php
	require('place.doc.php');
	//场地列表
	$app->get(
		'/place/list',
		function() use($app, $db2, $file_split, $file_subsplit){
			$data = $app->request()->get();
			$echo['error'] = "";
			if($data && isset($data['from']) && isset($data['to'])){
				if(isset($data['gymid'])){
					$set=array('gym_id'=>$data['gymid']);
				}
				if($data['news']==1 || $data['news']==2){
					$newsnum=$db2->select(2,'tb_news');
				}else{
					$newsnum=0;
				}
				if($data['news']==2){
					$echo['count'] = $newsnum;
				}else{
					$echo['count'] = $db2->select(2, 'tb_place', '*', $set)+$newsnum;
				}
				if($data['from']<$newsnum){
					$news=$db2->select(0,'tb_news','*',array('limit'=>array($data['from'],$newsnum-$data['from'])));
					foreach($news as $v){
						$tmp['placeid'] = $v['tbid'];
						$tmp['type'] = 2;
						$tmp['name'] = $v['title'];
						$dom = explode($file_subsplit, $v['img']);
						$tmp['img'] = $dom[0];
						$tmp['simg'] = getFileInfo($dom[0], 'simg');
						$tmp['people'] = 0;
						$tmp['price'] = 0;
						$echo['data'][] = $tmp;
						unset($tmp);
					}
				}
				if($data['from']+$data['to']>$newsnum && $data['news']!=2){
					$set['limit'] = array($data['from'], $data['to']);
					if(count($news)!=0){
						$set['limit'][1] = $data['to']-count($news);
					}else{
						$set['limit'][0] = $data['from']-$newsnum;
					}
					$place=$db2->select(0, 'tb_place', '*,('.$db2->select(4,'tb_gym','name',array('#tb_gym.tbid'=>'tb_place.gym_id')).')[gymname]',$set);
					foreach($place as $v){
						$tmp['placeid'] = $v['tbid'];
						$tmp['type'] = 1;
						$tmp['name'] = $v['name'];
						$tmp['gymname'] = $v['gymname'];
						$dom = explode($file_subsplit, $v['img']);
						$tmp['img'] = $dom[0];
						$tmp['simg'] = getFileInfo($dom[0], 'simg');
						$tmp['people'] = $v['people'];
						$tmp['price'] = $v['price'];
						$echo['data'][] = $tmp;
						unset($tmp);
					}
				}
			}
			echo json_encode($echo);
		}
	);
	//场地详情
	$app->get(
		'/place/info',
		function() use($app, $db2, $file_split, $file_subsplit){
			$data = $app->request()->get();
			$echo['error'] = "";
			if($data && isset($data['placeid'])){
				$place = $db2->select(1, 'tb_place', '*', array('tbid' => $data['placeid']));
				$echo['name'] = $place['name'];
				$dom = explode($file_subsplit, $place['img']);
				$echo['img'] = $dom[0];
				$echo['simg'] = getFileInfo($dom[0], 'simg');
				$echo['intro'] = $place['intro'];
				$echo['people'] = $place['people'];
				$echo['price'] = $place['price'];
			}else{
				$echo['error'] = "缺少参数，请检查";
			}
			echo json_encode($echo);
		}
	);
	//预约场地详情
	$app->get(
		'/place/reserveinfo',
		function() use($app, $db2, $file_split, $file_subsplit){
			$data = $app->request()->get();
			$echo['error'] = "";
			if($data && isset($data['placeid'])){
				$place = $db2->select(1, 'tb_place_snap', '*', array('tbid' => $data['placeid']));
				$echo['name'] = $place['name'];
				$dom = explode($file_subsplit, $place['img']);
				$echo['img'] = $dom[0];
				$echo['simg'] = getFileInfo($dom[0], 'simg');
				$echo['intro'] = $place['intro'];
				$echo['people'] = $place['people'];
				$echo['price'] = $place['price'];
				$echo['reservedt'] = $place['reservedt'];
				$echo['reservetime'] = $place['reservetime'];
			}else{
				$echo['error'] = "缺少参数，请检查";
			}
			echo json_encode($echo);
		}
	);
	//场地时间详情
	$app->get(
		'/place/time',
		function() use($app, $db2, $file_split, $file_subsplit){
			$data = $app->request()->get();
			$echo['error'] = "";
			if($data && isset($data['placeid']) && isset($data['userid']) && isset($data['usertype'])){
				$place = $db2->select(1, 'tb_place', '*', array('tbid' => $data['placeid']));
				$gym = $db2->select(1, 'tb_gym', '*', array('tbid' => $place['gym_id']));
				$peak=date('H',strtotime($gym['peaktime']));
				$off=date('H',strtotime($gym['offpeaktime']));
				if($data['usertype']==2){
					$day=30;
				}else{
					if($db2->select(3, 'tb_member', '(case when date_format(privilege_dt,"%Y-%m-%d")>=curdate() then 1 else 0 end) as isprivilege', array('tbid' => $data['userid']))==1){
						$day=7;
					}else{
						$day=3;
					}
				}
				for($i=1;$i<=$day;$i++){
					$tmp=array();
					$date=date('Y-m-d',strtotime("+".$i." day"));
					$tmp['date']=$date;
					$week = strtolower(date("l", strtotime($date)));
					$temptime= explode(',', $place[$week]);
					foreach($temptime as $value){
						if(!empty($value)){
							if($value<=$off && $value>=$peak){
								$money=$place['peak_price'];
							}else{
								$money=$place['price'];
							}
							$tmp['time'][$value]['money']=$money;
							$tmp['time'][$value]['state']=1;
						}
					}
					$week=date('w',strtotime($date));
					switch($week){
						case 0:$week='日';break;
						case 1:$week='一';break;
						case 2:$week='二';break;
						case 3:$week='三';break;
						case 4:$week='四';break;
						case 5:$week='五';break;
						case 6:$week='六';break;
					}
					$tmp['week'] = '星期'.$week;
					$spectime = $db2->select(1, 'tb_place_spectime', '*', array('place_id' => $data['placeid'], 'dt' => $date));
					if($spectime){
						$temptime = explode(',', $spectime);
						foreach($temptime as $value){
							if($value<=$off && $value>=$peak){
								$money=$place['peak_price'];
							}else{
								$money=$place['price'];
							}
							$tmp['time'][$value]['money']=$money;
							$tmp['time'][$value]['state']=1;
						}
					}
					$ordertime=$db2->select(0,'tb_place_snap',array('[><]tb_order'=>array('idcode')),'reservetime,userid,usertype',array('place_id'=>$data['placeid'],'reservedt' => $date,'state[!]'=>array(0,1)));
					foreach($ordertime as $value){
						$tmp['time'][$value['reservetime']]['state']=2;
						if($value['userid']==$data['userid'] && $value['usertype']==$data['usertype']){
							$tmp['time'][$value['reservetime']]['state']=3;
						}
					}
					$echo['data'][]=$tmp;
				}
			}else{
				$echo['error'] = "缺少参数，请检查";
			}
			echo json_encode($echo);
		}
	);
	//我的场地
	$app->get(
		'/place/mine',
		function() use($app, $db2){
			$data = $app->request()->get();
			$echo['error'] = "";
			if($data && isset($data['from']) && isset($data['to']) && isset($data['userid']) && isset($data['usertype'])){
				$where=array('tb_order.state'=>3,'tb_order.userid' => $data['userid'],'tb_order.usertype' =>$data['usertype'],'order'=>'tb_order.dt desc');
				if($data['type']==0){
					$where["(".$db2->select(4,'tb_place_curriculum','count(*)',array('#tb_place_curriculum.place_id'=>'tb_place_snap.tbid')).")"]=0;
					$where["#STR_TO_DATE(CONCAT(tb_place_snap.reservedt,' ',tb_place_snap.reservetime),'%Y-%m-%d %H')[>=]"]="DATE_ADD(now(),INTERVAL 1 HOUR)";
				}else if($data['type']==1){
					$where["(".$db2->select(4,'tb_place_curriculum','count(*)',array('#tb_place_curriculum.place_id'=>'tb_place_snap.tbid')).")[!]"]=0;
					$where["#STR_TO_DATE(CONCAT(tb_place_snap.reservedt,' ',tb_place_snap.reservetime),'%Y-%m-%d %H')[>=]"]="DATE_ADD(now(),INTERVAL 1 HOUR)";
				}else if($data['type']==2){
					$where["#STR_TO_DATE(CONCAT(tb_place_snap.reservedt,' ',tb_place_snap.reservetime),'%Y-%m-%d %H')[<]"]="DATE_ADD(now(),INTERVAL 1 HOUR)";
				}
				$echo['count'] = $db2->select(2, 'tb_place_snap',array("[><]tb_order"=>array('idcode')), '*', $where);
				$where['limit'] = array($data['from'], $data['to']);
				$place = $db2->select(0, 'tb_place_snap',array("[><]tb_order"=>array('idcode')),'tb_place_snap.tbid,tb_place_snap.name,tb_place_snap.intro,tb_place_snap.people,tb_place_snap.price,tb_place_snap.reservedt,tb_place_snap.reservetime,tb_place_snap.place_id,case when STR_TO_DATE(CONCAT(tb_place_snap.reservedt," ",tb_place_snap.reservetime),"%Y-%m-%d %H")>=DATE_ADD(now(),INTERVAL 1 HOUR) then 1 else 0 end as isprocessing,('.$db2->select(4,'tb_place_curriculum_snap','count(*)',array('#tb_place_curriculum_snap.place_id'=>'tb_place_snap.tbid')).')[count],('.$db2->select(4,'tb_place_curriculum','isfree',array('#tb_place_curriculum.place_id'=>'tb_place_snap.tbid')).')[isfree],('.$db2->select(4,'tb_place_curriculum','title',array('#tb_place_curriculum.place_id'=>'tb_place_snap.tbid')).')[title],('.$db2->select(4,'tb_gym','name',array('#tb_gym.tbid'=>'tb_place_snap.gym_id')).')[gymname]',$where);
				foreach($place as $places){
					$tmp['tbid'] = $places['tbid'];
					$tmp['title'] = $places['title'];
					$tmp['name'] = $places['name'];
					$tmp['intro'] = $places['intro'];
					$tmp['people'] = $places['people'];
					$tmp['price'] = $places['price'];
					$tmp['placeid'] = $places['place_id'];
					$tmp['reservedt'] = $places['reservedt'];
					$tmp['reservetime'] = $places['reservetime'];
					$tmp['count'] = $places['count'];
					$tmp['isfree'] = $places['isfree'];
					$tmp['gymname'] = $places['gymname'];
					$tmp['isprocessing'] = $places['isprocessing'];
					$week=date('w',strtotime($places['reservetime']));
					switch($week){
						case 0:$week='日';break;
						case 1:$week='一';break;
						case 2:$week='二';break;
						case 3:$week='三';break;
						case 4:$week='四';break;
						case 5:$week='五';break;
						case 6:$week='六';break;
					}
					$tmp['week'] = '星期'.$week;
					$echo['data'][]=$tmp;
				}
			}else{
				$echo['error'] = "缺少参数，请检查";
			}
			echo json_encode($echo);
		}
	);

	//场地课程详情
	$app->get(
		'/place/curriculum/info',
		function() use($app, $db2, $file_split, $file_subsplit){
			$data = $app->request()->get();
			$echo['error'] = "";
			if($data && isset($data['curriculumid'])){
				$place = $db2->select(1, 'tb_place_curriculum', '*,('.$db2->select(2,'tb_place_curriculum_snap','*',array('place_curriculum_id'=>$data['curriculumid'])).')[count]', array('tbid' => $data['curriculumid']));
				$echo['title'] = $place['title'];
				$echo['img'] = SITEROOTURL.$place['img'];
				$echo['intro'] = $place['intro'];
				$echo['introimg'] = $place['introimg'];
				$echo['isfree'] = $place['isfree'];
				$echo['people'] = $place['people'];
				$echo['price'] = $place['price'];
				$echo['count'] = $place['count'];
			}else{
				$echo['error'] = "缺少参数，请检查";
			}
			echo json_encode($echo);
		}
	);
	//场地课程添加
	$app->post(
		'/place/curriculum/add',
		function() use($app, $db2){
			$data = $app->request()->post();
			$echo['error'] ='';
			if($data && isset($data['coachid']) && isset($data['placeid']) && isset($data['type']) && isset($data['title']) && isset($_FILES['img']) && isset($data['price']) && isset($data['people'])){
				if($db2->select(2,'tb_place_curriculum','*',array('place_id'=>$data['placeid']))){
					$echo['error'] = "该场地已发布课程，不允许重复发布";
				}else{
					$imgpath = 'dofiles/coach/'.$data['coachid'].'/place/curriculum/';
					create($imgpath);
					$imgurl = $imgpath.md5(uniqid(rand())).'.jpg';
					move_uploaded_file($_FILES['img']['tmp_name'], $imgurl);
					$set = array(
						'type' => $data['type'],
						'title' => $data['title'],
						'img' => $imgurl,
						'price' => $data['price'],
						'people' => $data['people'],
						'coach_id' => $data['coachid'],
						'isfree' => $data['isfree'],
						'place_id' => $data['placeid']
					);
					if(isset($data['intro'])){
						$set['intro'] = $data['intro'];
					}
					if(!empty($_FILES['introimg'])){
						$introimgurl = creatfilesimg($_FILES['introimg'],'',$data['coachid'],2,'place/curriculum');
						$set['introimg'] = $introimgurl;
					}
					$curriculumid = $db2->insert('tb_place_curriculum', $set);
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);

	//我的场地课程
	$app->get(
		'/place/curriculum/mine',
		function() use($app, $db2, $file_split, $file_subsplit){
			$data = $app->request()->get();
			$echo['error'] = "";
			if($data && isset($data['from']) && isset($data['to']) && isset($data['memberid'])){
				$where=array('order'=>'tb_place_snap.reservedt desc,reservetime desc','tb_order.userid'=>$data['memberid'],'tb_order.usertype'=>1,'tb_order.state'=>3);
				$echo['count'] = $db2->select(2, 'tb_place_curriculum_snap',array("[><]tb_order"=>array('idcode'),"[><]tb_place_snap"=>array('place_id'=>'tbid')), '*', $where);
				$where['limit'] = array($data['from'], $data['to']);
				$curriculum =$db2->select(0,'tb_place_curriculum_snap',array("[><]tb_order"=>array('idcode'),'[><]tb_place_snap'=>array('place_id'=>'tbid')),'tb_place_curriculum_snap.tbid,tb_place_curriculum_snap.place_curriculum_id,tb_place_curriculum_snap.title,tb_place_snap.name,tb_place_curriculum_snap.intro,tb_place_curriculum_snap.people,tb_place_curriculum_snap.price,tb_place_curriculum_snap.place_id,tb_place_snap.reservedt,tb_place_snap.reservetime,tb_place_curriculum_snap.isfree,('.$db2->select(4,'tb_place_curriculum_snap','count(*)',array('#tb_place_curriculum_snap.place_id'=>'tb_place_snap.tbid')).')[count],('.$db2->select(4,'tb_gym','name',array('#tb_gym.tbid'=>'tb_place_snap.gym_id')).')[gymname],case when STR_TO_DATE(CONCAT(tb_place_snap.reservedt," ",tb_place_snap.reservetime),"%Y-%m-%d %H")>=DATE_ADD(now(),INTERVAL 1 HOUR) then 1 else 0 end as isprocessing',$where);
				foreach($curriculum as $value){
					$tmp['tbid'] = $value['tbid'];
					$tmp['place_curriculum_id'] = $value['place_curriculum_id'];
					$tmp['title'] = $value['title'];
					$tmp['name'] = $value['name'];
					$tmp['intro'] = $value['intro'];
					$tmp['people'] = $value['people'];
					$tmp['price'] = $value['price'];
					$tmp['placeid'] = $value['place_id'];
					$tmp['reservedt'] = $value['reservedt'];
					$tmp['reservetime'] = $value['reservetime'];
					$tmp['count'] = $value['count'];
					$tmp['isfree'] = $value['isfree'];
					$tmp['gymname'] = $value['gymname'];
					$tmp['isprocessing'] = $value['isprocessing'];
					$week=date('w',strtotime($value['reservetime']));
					switch($week){
						case 0:$week='日';break;
						case 1:$week='一';break;
						case 2:$week='二';break;
						case 3:$week='三';break;
						case 4:$week='四';break;
						case 5:$week='五';break;
						case 6:$week='六';break;
					}
					$tmp['week'] = '星期'.$week;
					$echo['data'][]=$tmp;
				}
				
				if($curriculum){
    				//红色提醒栏-今日课程
    				$tmp = date("Y-m-d");
    				$begin_dt = $tmp." 00:00";
    				$end_dt = $tmp." 23:59";
    				$sql = "select a.tbid,b.title,b.intro,(select nickname from tb_coach  where tb_coach.tbid = b.coach_id   ) as coachname".
    				    ",(select nickname from tb_member  where tb_member.tbid = a.member_id   ) as membername".
    				    ",a.state,b.type,a.dt from tb_reserve as a  inner join tb_place_curriculum_snap as b on a.curriculum_id=b.tbid ".
    				    "where a.member_id = ? and a.state=1 and a.type=1 and a.dt >= ? and a.dt <= ?  order by a.dt asc";
    				$ht = $db2->prepare($sql);
    				$ht->execute(array($data['memberid'], $begin_dt, $end_dt));
    				$tmp = $ht->fetchAll(PDO::FETCH_ASSOC);
    				
    				if($tmp){
    				    foreach($tmp as $key => &$value){
    				        $value['reservedt'] = date('Y-m-d',strtotime($value['dt']));
    				        $value['reservetime'] = date('H',strtotime($value['dt']));
    				    }
    				    $echo['today'] = $tmp;
    				}else{
    				    $echo['today'] = array();
    				}
				}
			}else{
				$echo['error'] = "缺少参数，请检查";
			}
			echo json_encode($echo);
		}
	);
?>
